package com.worthtech.billportal.dao.hibernate;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.worthtech.billportal.bean.CsCtrlOrganization;
import com.worthtech.billportal.dao.CtrlGroupDAO;
import com.worthtech.billportal.exception.SCException;

public class CtrlGroupHibernateDAO extends HibernateDaoSupport implements
		CtrlGroupDAO {

	public CsCtrlOrganization getOrgan(String groupid) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlOrganization where code = ?", groupid);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return (CsCtrlOrganization) list.get(0);
		}
	}

	public List getList(String orgcode) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlOrganization where orgcode like ?", orgcode);
		return list;
	}

	public void updateInfo(CsCtrlOrganization organ) throws SCException {
		this.getHibernateTemplate().update(organ);
	}

	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if (perNo == 0) {
			return this.getSession().createQuery(hql).list();
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public String getCount(String tableName, String condition) {
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
		System.out.println("count=" + tmp);
		return tmp;
	}

	public void saveInfo(CsCtrlOrganization organ) {
		this.getHibernateTemplate().save(organ);
	}

	public void delInfo(CsCtrlOrganization organ) {
		this.getHibernateTemplate().delete(organ);
	}

	public String getSeqID(String seqname) {
		return this.getSession().createSQLQuery(
				"SELECT " + seqname + ".NEXTVAL FROM USER_SEQUENCES").list()
				.get(0).toString();
	}

	public CsCtrlOrganization getOrgByID(String orgcode) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlOrganization where orgcode = ?", orgcode);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return (CsCtrlOrganization) list.get(0);
		}
	}

}
